package pl.edu.agh.ssm.monitor.data;

import java.net.InetAddress;
import java.rmi.server.UID;
import java.util.LinkedList;
import java.util.List;
//import java.util.List;

/**
 * Connection between session participants.
 * This objects contains information passing between participants,
 * like rtcp reports or last media packets.
 * @author Tomasz Jadczyk
 *
 */
public class SessionConnection implements SessionData{

	/* Connection participants */
	private SessionNode sender;
	private SessionNode receiver;
	/* Media description - codec name, media type, sampling frequency*/
	private SessionMedia mediaDesc;
	/* Last RTP Packet from Sender */
	private RTPPacket lastRtpPacket;
	private List<RTSPPacket> rtspPackets = new LinkedList<RTSPPacket>();
	/* Receiver report */
	private ReceiverReportInfo receiverReportInfo;
	private long lastReceiverReportTime;
	/* Connection informations*/
	private InetAddress connectionAddress ;
	private int connectionPort;
	
	private String id;
	private String sessionID;
	
	//TODO change Packet to other type which will be printable
//	private List<Packet> lastPackets;
	public SessionConnection() {
		
	}
	
	public SessionConnection(SessionNode sender, SessionNode receiver, String sessionID) {
		this.sender = sender;
		this.receiver = receiver;
		this.sessionID = sessionID;
		this.id = new UID().toString();
	}
	 
	public SessionConnection(SessionNode sender, SessionNode receiver,
			InetAddress connectionAddress, int connectionPort, String sessionID) {
		super();
		this.sender = sender;
		this.receiver = receiver;
		this.connectionAddress = connectionAddress;
		this.connectionPort = connectionPort;
		this.sessionID = sessionID;
		this.id = new String(String.valueOf(System.currentTimeMillis()) + sender.getId() + receiver.getId());
	}

	public SessionNode getSender() {
		return sender;
	}
	
	public void setSender(SessionNode sender) {
		this.sender = sender;
	}
	
	public SessionNode getReceiver() {
		return receiver;
	}
	
	public void setReceiver(SessionNode receiver) {
		this.receiver = receiver;
	}
		
	
	public ReceiverReportInfo getReceiverReport() {
		return receiverReportInfo;
	}
	public void setMediaReport(ReceiverReportInfo receiverReportInfo) {
		this.receiverReportInfo = receiverReportInfo;
	}
//	public List<Packet> getLastPackets() {
//		return lastPackets;
//	}
//	
//	public void clearPackets() {
//		lastPackets.clear();
//	}
//	
//	public void addPacket(Packet packet) {
//		lastPackets.add(packet);
//	}

	public SessionMedia getMediaDesc() {
		return mediaDesc;
	}

	public void setMediaDesc(SessionMedia mediaDesc) {
		this.mediaDesc = mediaDesc;
	}

	public InetAddress getConnectionAddress() {
		return connectionAddress;
	}

	public void setConnectionAddress(InetAddress connectionAddress) {
		this.connectionAddress = connectionAddress;
	}

	public int getConnectionPort() {
		return connectionPort;
	}

	public void setConnectionPort(int connectionPort) {
		this.connectionPort = connectionPort;
	}

	@Override
	public String getId() {
		return id;
	}

	public String getSessionId() {
		return sessionID;
	}

	public RTPPacket getLastRtpPacket() {
		return lastRtpPacket;
	}

	public void setLastRtpPacket(RTPPacket lastRtpPacket) {
		this.lastRtpPacket = lastRtpPacket;
	}

	public long getLastReceiverReportTime() {
		return lastReceiverReportTime;
	}

	public void setLastReceiverReportTime(long lastReceiverReportTime) {
		this.lastReceiverReportTime = lastReceiverReportTime;
	}

	public List<RTSPPacket> getRtspPackets() {
		return rtspPackets;
	}
	
	public RTSPPacket getLastRtspPacket() {
		
		if (rtspPackets.size() > 0)
			return rtspPackets.get(rtspPackets.size() - 1);
		
		return null;
	}
	
	public void addRtspPacket(RTSPPacket packet) {
		System.out.println("Add rtsp packet to connection");
		rtspPackets.add(packet);
	}
}
